package it.gnocco.log;

import it.gnocco.properties.Property;

import java.io.*;
import java.util.*;

public class Logger {
	static File file = new File("C:\\log\\ML.log");
	static FileWriter fw = null;
	static BufferedWriter bw = null;
	public static final int INFO = 5;
	public static final int MESSAGE = 4;
	public static final int WARNING = 3;
	public static final int ERROR = 2;
	public static final int info = 5;
	public static final int message = 4;
	public static final int warning = 3;
	public static final int error = 2;
	private static int _level = 3;
	private static boolean _azzera = true;
	
	public Logger(Property prop){
		this(prop.get("name_log_file"), prop.get("log_level"), prop.get("azzera_log_file"));
	}
	
	public Logger(int level){
		this(new File("C:\\log\\ML.log"), level);
	}

	private Logger(String f, String level, String azzera){
		if(!f.equals(""))
			file = new File(f);
		if(!level.equals(""))
			_level = Integer.valueOf(level);
		if(!azzera.equals(""))
			_azzera = Boolean.valueOf(azzera);
		try {
			fw = new FileWriter(file, !_azzera);
			bw = new BufferedWriter(fw);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Logger(File f, int level, boolean azzera){
		file = f;
		_level = level;
		try {
			fw = new FileWriter(file, !_azzera);
			bw = new BufferedWriter(fw);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	public Logger(File f, int level){
		file = f;
		_level = level;
		try {
			fw = new FileWriter(file, !_azzera);
			bw = new BufferedWriter(fw);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void log(int level, String classe, String method, String message){
		if(level <= _level){
			Calendar calendar = new GregorianCalendar();
			int ore = calendar.get(Calendar.HOUR);
			int minuti = calendar.get(Calendar.MINUTE);
			int secondi = calendar.get(Calendar.SECOND);
			int millisecondi = calendar.get(Calendar.MILLISECOND);
			int giorno = calendar.get(Calendar.DAY_OF_MONTH);
			int mese = calendar.get(Calendar.MONTH) +1;
			int anno = calendar.get(Calendar.YEAR);
			
			if(calendar.get(Calendar.AM_PM) != 0)
				ore = ore + 12;
			String ORE = Integer.toString(ore);
			if (ore<10)
				ORE = "0"+ORE;
			String MINUTI = Integer.toString(minuti);
			if (minuti<10)
				MINUTI = "0"+MINUTI;
			String SECONDI = Integer.toString(secondi);
			if (secondi<10)
				SECONDI = "0"+SECONDI;
			String GIORNO = Integer.toString(giorno);
			if (giorno<10)
				GIORNO = "0"+GIORNO;
			String MESE = Integer.toString(mese);
			if (mese<10)
				MESE = "0"+MESE;
			String data = GIORNO + "/" + MESE + "/" + anno + " " + ORE + ":" + MINUTI + ":" + SECONDI + ":" + millisecondi;
		    String type = "";
			if(level==5){
				type = "INFO";
			} else if (level==4){
				type = "MESSAGE";
			} else if (level==3){
				type = "WARNING";
			} else if (level==2){
				type = "ERROR";
			}
			try {
		    	bw.write(data + " "+ type +" - " +" METHOD = " + classe +"." +method + "(): "+ message + "\n");
		    	bw.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }
	}
}
